﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Vlc.DotNet.Core;

namespace vlcwinForm
{
    public partial class Form1 : Form
    {        
        List<string> playList = new List<string>();//视频路径的列表 
        string[] allFiles;//所有文件名       
        public Form1()
        {
            InitializeComponent();           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            vlcControl1.EndReached += vlcControl1_EndReached1;

        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                //ofd.Filter = "mp4文件|*.mp4|avi 文件|*.avi|wmv文件|*.wmv";
                ofd.Filter = "(*.mp4;*.avi;*wmv)|*.mp4;*.avi;*.wmv";
                if (ofd.ShowDialog(this) == DialogResult.OK)
                {
                    vlcControl1.Play(new System.IO.FileInfo(ofd.FileName));                                     
                }
            }
        }



        private void vlcControl1_VlcLibDirectoryNeeded(object sender, Vlc.DotNet.Forms.VlcLibDirectoryNeededEventArgs e)
        {
            var currentAssembly = Assembly.GetEntryAssembly();
            var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
            // Default installation path of VideoLAN.LibVLC.Windows
            e.VlcLibDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));

            if (!e.VlcLibDirectory.Exists)
            {
                var folderBrowserDialog = new FolderBrowserDialog();
                folderBrowserDialog.Description = "Select Vlc libraries folder.";
                folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
                folderBrowserDialog.ShowNewFolderButton = true;
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    e.VlcLibDirectory = new DirectoryInfo(folderBrowserDialog.SelectedPath);
                }
            }
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            vlcControl1.Play();
            
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            vlcControl1.Pause();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            vlcControl1.Stop();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.Filter = "png文件|*.png|jpg 文件|*.jpg";
                if (sfd.ShowDialog(this) == DialogResult.OK)
                {                   
                    vlcControl1.TakeSnapshot(sfd.FileName);
                }
            }
        }     
        private void button2_Click(object sender, EventArgs e)
        {           
            playList.Clear();           
            allFiles = Directory.GetFiles("./video");
            foreach (string strFile in allFiles)
            {
               string tempstr=strFile.ToLower();
                if(tempstr.EndsWith(".mp4") |
                    tempstr.EndsWith(".avi")| 
                    tempstr.EndsWith(".wmv")|
                    tempstr.EndsWith("flv")|
                    tempstr.EndsWith("rmvb"))
                playList.Add(strFile);
            }
            WriteLog("总视频播数:" + playList.Count.ToString());
            // 播放列表中的第一个视频
            if (playList.Count > 0)
            {
                WriteLog("当前播放的文件名:" + playList[0]);
                vlcControl1.SetMedia(new System.IO.FileInfo(playList[0]));
                vlcControl1.Play();

            }
            else {

                MessageBox.Show("video目录下没有视频", "警告", MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
            }            
        }

        // 在控件EndReached的事件函数中加入如下代码 
        private void vlcControl1_EndReached(object sender, Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e)
        {
            playList.RemoveAt(0);                 
            if (playList.Count > 0)
            {              
                // 核心代码
                ThreadPool.QueueUserWorkItem(_ => this.vlcControl1.Play(new System.IO.FileInfo(playList[0])));
                WriteLog("剩余视频播数:" + playList.Count.ToString());
               
            }
            if (playList.Count == 0) {
                allFiles = Directory.GetFiles("./video");
                foreach (string strFile in allFiles)
                {
                    string tempstr = strFile.ToLower();
                    if (tempstr.EndsWith(".mp4")|
                        tempstr.EndsWith(".avi")|
                        tempstr.EndsWith(".wmv")|
                        tempstr.EndsWith("flv")|
                         tempstr.EndsWith("rmvb")
                        )
                        playList.Add(strFile);
                }
                WriteLog("当前播放的文件名:" + playList[0]);
                ThreadPool.QueueUserWorkItem(_ => this.vlcControl1.Play(new System.IO.FileInfo(playList[0])));
            }
        }

        private void vlcControl1_EndReached1(object sender, Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e)
       {
            if (playList.Count > 0)
            {
                string tempfile = playList[0];
                playList.RemoveAt(0);
                playList.Add(tempfile);
                ThreadPool.QueueUserWorkItem(_ => this.vlcControl1.Play(new System.IO.FileInfo(playList[0])));
                WriteLog("当前播放的文件名:" + playList[0]);
            }
        }
        public void WriteLog(string msg)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            try
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "消息：" + msg);                                                  
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (IOException e)
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + "异常：" + e.Message);                                                 
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        }
    }
}
